package com.thering;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * author: the_ring
 * date: 2022/5/27
 * function: Type Count
 */
public class TypeCountReducer extends TableReducer<Text, IntWritable, ImmutableBytesWritable> {

    private byte[] rowKey;
    private byte[] familyName = "cf".getBytes(StandardCharsets.UTF_8);

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        Configuration conf = context.getConfiguration();
        this.rowKey = conf.get("date").getBytes(StandardCharsets.UTF_8);
    }

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        Integer count = 0;
        for (IntWritable value : values) {
            count++;
        }

        Put put = new Put(rowKey);
        put.addColumn(familyName, Bytes.toBytes(key.toString()), Bytes.toBytes(count));

        context.write(null, put);
    }

}